package com.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import com.stock.Stock;


// this class parse the stocks data
public class Feeder {

	// read the data folder and return the parsed data
	public static List<Stock> Feed(String dataFolder, List<String> stockList ){

		List<Stock> res = new  LinkedList<Stock>();

		for (String stockName : stockList) {
			String fileFullPath = dataFolder + "\\" + stockName; 
			Stock stock= new Stock(stockName);
			try {
				BufferedReader br = new BufferedReader( new FileReader( fileFullPath ));
				String currLine; 
				while( (currLine = br.readLine()) != null){
					StringTokenizer st =  new StringTokenizer(currLine, ",");
					st.nextToken();
					stock.addData(Long.valueOf(st.nextToken()), Double.valueOf(st.nextToken()),
								Double.valueOf(st.nextToken()), Double.valueOf(st.nextToken()),
								Double.valueOf(st.nextToken()), Long.valueOf(st.nextToken()));
				}
				br.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			res.add(stock);
		}

		return res;
	}


	
	public static List<String> ReadFilesNames(String filenames) {

	    List<String> stocks =  new LinkedList<String>()  ;
        try{
                FileInputStream fstream = new FileInputStream(filenames);
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                while ((strLine = br.readLine()) != null){
                	stocks.add(new String(strLine));
                }
                in.close();
        }catch (Exception e){
                e.printStackTrace();
        }
        
        return stocks;
	}
}
